package com.cong.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author cong
 * <p>
 * created on 2021/4/20 下午4:55
 */
public class ChannelDemo2 {
    public static void main(String[] args) throws IOException {
        String s = "ccccccccccccc";

        FileOutputStream fos = new FileOutputStream("/data/cong/channel1.txt");
        FileChannel channel = fos.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(s.getBytes());
        //为后面的输出做准备，更新了position和limit（最大可输出字节数）
        byteBuffer.flip();
        channel.write(byteBuffer);
        fos.close();
    }
}
